package com.guigui.gulimall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyRabbitConf {

//    @Autowired
//    private RabbitTemplate rabbitTemplate;

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange("order-event-exchange", true, false);
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue("order-release-order-queue", true, false, false);
    }

    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> args = new HashMap<>(3);
        args.put("x-dead-letter-routing-key", "order.release.order");
        args.put("x-dead-letter-exchange", "order-event-exchange");
        args.put("x-message-ttl", 1000 * 60 * 2);
        return new Queue("order-delay-queue", true, false, false, args);
    }

    @Bean
    public Binding orderReleaseOrderQueueBinding() {
        return new Binding("order-release-order-queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.order", null);
    }

    @Bean
    public Binding orderDelayQueueBinding() {
        return new Binding("order-delay-queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.create.order", null);
    }

    /**
     * 绑定订单路由
     */
    @Bean
    public Binding stockReleaseStockQueueBindingByOrderExchange() {
        return new Binding("stock-release-stock-queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.other.#", null);
    }

//    @PostConstruct
//    public void initRabbitTemplate() {
//
//        // 入队成功回调
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            @Override
//            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//                System.out.println("入队成功correlationData:" + correlationData + "\t ack:" + ack + "\tcause" + cause);
//            }
//        });
//        // 入队异常回调
//        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
//            @Override
//            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
//                System.out.println("入队失败message:" + message + "\t 相应码:" + replyCode + "\t 原因" + replyText + "\t 路由:" + exchange + "\t 路由键：" + routingKey);
//            }
//        });
//
//    }
}
